package src.main.java.com.pms.gui.dialog;

import src.main.java.com.pms.service.PetBasicService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SearchResultDialog extends JDialog {
    private JTable resultTable;
    private DefaultTableModel tableModel;
    private String searchName;
    private PetBasicService petService;

    public SearchResultDialog(JFrame parent, ResultSet rs, String searchName) {
        super(parent, "搜索结果", true);
        this.searchName = searchName;
        initComponents(rs);
        this.petService = new PetBasicService();
    }
    private void initComponents(ResultSet rs) {
        setSize(500, 300);
        setLocationRelativeTo(getParent());

        // 创建表格模型
        String[] columnNames = {"ID", "名称", "类型", "年龄"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        resultTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(resultTable);

        // 加载搜索结果
        try {
            while (rs.next()) {
                Object[] row = {
                    rs.getInt("id"),
                    rs.getString("name"),
                    rs.getString("type"),
                    rs.getInt("age")
                };
                tableModel.addRow(row);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "加载搜索结果失败：" + e.getMessage(),
                "错误", JOptionPane.ERROR_MESSAGE);
        }

        // 设置布局
        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);

        // 添加按钮面板
        JPanel buttonPanel = new JPanel();
        JButton closeButton = new JButton("关闭");
        JButton deleteButton = new JButton("删除");
        JButton editButton = new JButton("编辑");

        // 添加按钮点击事件处理
        closeButton.addActionListener(e -> dispose());
        deleteButton.addActionListener(e -> deleteSelectedPet());
        editButton.addActionListener(e -> {
            try {
                editSelectedPet();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        buttonPanel.add(closeButton);
        buttonPanel.add(deleteButton);
        buttonPanel.add(editButton);

        // 设置布局
        setLayout(new BorderLayout());
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }
    private void deleteSelectedPet() {
        int selectedRow = resultTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要删除的宠物！",
                    "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        int id = (Integer) tableModel.getValueAt(selectedRow, 0);
        int confirm = JOptionPane.showConfirmDialog(this,
                "确定要删除这只宠物吗？", "确认删除",
                JOptionPane.YES_NO_OPTION);

        if (confirm == JOptionPane.YES_OPTION) {
            try {
                if (petService.deletePet(id)) {
                    JOptionPane.showMessageDialog(this, "删除成功！");
                    // 重新加载搜索结果

                    ResultSet rs = petService.searchPetsByName(searchName);
                    loadSearchResult(rs);
                } else {
                    JOptionPane.showMessageDialog(this, "删除失败！",
                            "错误", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "删除失败：" + e.getMessage(),
                        "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void editSelectedPet() throws SQLException, ClassNotFoundException {
        int selectedRow = resultTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要编辑的宠物！",
                    "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        int id = (Integer) tableModel.getValueAt(selectedRow, 0);
        new EditPetDialog(this, id).setVisible(true);
        // 重新加载搜索结果
        ResultSet rs = petService.searchPetsByName(searchName);
        loadSearchResult(rs);
    }

    private void loadSearchResult(ResultSet rs) {
        try {
            tableModel.setRowCount(0);
            while (rs.next()) {
                Object[] row = {
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("type"),
                        rs.getInt("age")
                };
                tableModel.addRow(row);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "加载搜索结果失败：" + e.getMessage(),
                    "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}